#include "Tutorial_Lighting.h"

//
#include "Modules.h"

//
#include "pcrecpp.h"
//
#pragma comment (lib, "Euclid.lib")

//
Hannover::Hannover()
:	_modules(0)
{
	_windowTitle = "Tutorial_Lighting";
}

Hannover::~Hannover()
{

}

bool Hannover::createModules()
{
	_modules = new Modules;
	
	//
	if (!_modules->create(_hwnd))
	{
		return false;
	}

	//
	std::string pattern(".*3Dfx.*");
	pcrecpp::RE re(pattern);
	if (re.FullMatch("3dfx"))
	{
		breakable;
	}

	//
	return true;
}

bool Hannover::foreRender()
{
	//
	if (!_modules->getRenderEngine()->getRenderSystem()->setRenderState(D3DRS_LIGHTING, true))
	{
		return false;
	}

	////
	//if (!_modules->getRenderEngine()->getRenderSystem()->setRenderState(D3DRS_BLENDOPALPHA, true))
	//{
	//	return false;
	//}

	//
	if (!_modules->getRenderEngine()->getRenderSystem()->setRenderState(D3DRS_AMBIENT, 0xff202ff0))
	{
		return false;
	}

	//
	//
	if (!_modules->getRenderEngine()->getRenderSystem()->setRenderState(D3DRS_CULLMODE, D3DCULL_CCW))
	{
		return false;
	}

	////
	//if (!_modules->getRenderEngine()->getRenderSystem()->setRenderState(D3DRS_SPECULARENABLE, true))
	//{
	//	return false;
	//}

	//
	return true;
}

bool Hannover::rendering()
{
	//
	_modules->getRenderEngine()->getRenderSystem()->beginFrame(true, true, true);

	//initGeometry();
	renderTiger();

	//
	_modules->getRenderEngine()->getRenderSystem()->endFrame();

	//
	return true;
}

bool Hannover::postRender()
{
	//
	return true;
}

bool Hannover::forePlay()
{
	//
	if (!createWindow())
	{
		return false;
	}

	//
	if (!adjustClientArea())
	{
		return false;
	}

	//
	if (!createModules())
	{
		return false;
	}

	//
	if (!setViewport())
	{
		return false;
	}
	
	//
	return true;
}

bool Hannover::postPlay()
{
	//
	if (_modules)
	{
		_modules->close();
	}

	//
	return true;
}

bool Hannover::update()
{
	if (NULL == _modules)
	{
		return false;
	}

	if (!_modules->getRenderEngine()->getRenderSystem()->update())
	{
		return false;
	}

	//
	if (!Buddha::Mouse::getSingletonPtr()->update())
	{
		return false;
	}

	//
	return true;
}

bool Hannover::setViewport()
{
	//
	D3DVIEWPORT9 v;
	v.X = 0;
	v.Y = 0;
	v.Width = Buddha::WindowHelper::getSingletonPtr()->getClientWidth(_hwnd);
	v.Height = Buddha::WindowHelper::getSingletonPtr()->getClientHeight(_hwnd);
	v.MinZ = 0.0f;
	v.MaxZ = 1.0f;

	//
	if (!_modules->getRenderEngine()->getRenderSystem()->setViewport(&v))
	{
		return false;
	}

	//
	return true;
}

bool Hannover::initGeometry()
{
	//
	Euclid::Material yellow = Euclid::Material::Yellow;
	_modules->getRenderEngine()->getRenderSystem()->setMaterial(&yellow);

	//
	Euclid::Light li = Euclid::Light::White;
	_modules->getRenderEngine()->getRenderSystem()->setLight(0, &li);
	
	_modules->getRenderEngine()->getRenderSystem()->setVertexDeclaration(Euclid::VD_POSITION_NORMAL);

	//
	static int NUM = 100;
	const float radius = 10.0f;
	//
	Euclid::POSITION_NORMAL* v = new Euclid::POSITION_NORMAL[NUM * 2];
	memset(v, 0, 2 * NUM * sizeof(Euclid::POSITION_NORMAL));
	//
	float angle = 0.0f;
	for (int i = 0; i < NUM; ++i)
	{
		//
		angle = 2 * D3DX_PI * i / (NUM-1);

		//
		v[2 * i] = Euclid::POSITION_NORMAL(D3DXVECTOR3(radius * cos(angle), radius*0.5f, radius * sin(angle)),
			D3DXVECTOR3(cos(angle), 0.0f, sin(angle)));

		//
		v[2 * i + 1] = Euclid::POSITION_NORMAL(D3DXVECTOR3(radius * cos(angle), -radius*0.5f, radius * sin(angle)),
			D3DXVECTOR3(cos(angle), 0.0f, sin(angle)));
	}

	//
	_modules->getRenderEngine()->getRenderSystem()->drawPrimitiveUP(D3DPT_TRIANGLESTRIP, 2*NUM, v, sizeof(Euclid::POSITION_NORMAL));
	//
	_modules->getRenderEngine()->getRenderSystem()->setVertexDeclaration(Euclid::VD_NULL);

	//
	return true;
}

void Hannover::renderTiger()
{
	//
	//
	Euclid::Light li = Euclid::Light::White;
	_modules->getRenderEngine()->getRenderSystem()->setLight(0, &li);
	Euclid::Mesh m;
	std::string tiger;
	Buddha::FileSystem::getSingletonPtr()->getDataDirectory(tiger);
	tiger += "\\image\\tiger.x";
	m.loadMeshFromX(tiger);
	m.render();
}